package com.mlnx.device.dao.mongodb.iml;

import com.mlnx.common.utils.MyLog;
import com.mlnx.device.dao.mongodb.DeviceStateDao;
import com.mlnx.device.pojo.dto.DeviceStateRecord;
import com.mlnx.mptp.model.DeviceState;
import com.mlnx.service.base.properties.MutilDeviceProperties;

import org.shan.spring.mongodb.MongodbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

/**
 * Created by amanda.shan on 2019/11/26.
 */
@Repository
public class DeviceStateDaoIml implements DeviceStateDao {

    private MyLog log = MyLog.getLog(getClass());

    @Autowired
    private MutilDeviceProperties mutilDeviceProperties;

    @PostConstruct
    @Override
    public void init() {

        log.info("开始初始化devicestate mongodb存储");

        // 设置索引
        Map<String, Sort.Direction> map = new HashMap<>();
        map.put("patientId", Sort.Direction.ASC);
        map.put("date", Sort.Direction.ASC);
        map.put("deviceState", Sort.Direction.ASC);
        MongodbUtils.setIndex(mutilDeviceProperties.getStore().getDeviceStateCollectionName(), map, true);

        log.info("初始化devicestate mongodb存储结束");
    }

    @Override
    public void save(DeviceStateRecord deviceStateRecord) {
        MongodbUtils.save(deviceStateRecord, mutilDeviceProperties.getStore().getDeviceStateCollectionName());
    }

    @Override
    public List<DeviceStateRecord> selectOnOffLine(Date startTime, Date endTime, Integer patientId) {

        List condition = new ArrayList();
        condition.add(DeviceState.DEVICE_ONLINE.toString());
        condition.add(DeviceState.DEVICE_OFFLINE.toString());

        Criteria criteria = Criteria.where("patientId").is(patientId);
        criteria.and("date").gt(startTime).lt(endTime).and("deviceState").in(condition);

        Query query = Query.query(criteria);
        query.with(new Sort(Sort.Direction.ASC, "date"));

        List<DeviceStateRecord> records = MongodbUtils.find(DeviceStateRecord.class, query,
                mutilDeviceProperties.getStore().getDeviceStateCollectionName());
        return records;
    }

    @Override
    public List<DeviceStateRecord> selectEcgEvents(Date startTime, Date endTime, Integer patientId) {
        Criteria criteria = Criteria.where("patientId").is(patientId);
        criteria.and("date").gte(startTime).lte(endTime)
                .and("deviceState").in(DeviceState.EXIGENCY_STATE.name(), DeviceState.ABNORMAL_MARK.name());

        Query query = Query.query(criteria);
        query.with(new Sort(Sort.Direction.ASC, "date"));

        List<DeviceStateRecord> records = MongodbUtils.find(DeviceStateRecord.class, query,
                mutilDeviceProperties.getStore().getDeviceStateCollectionName());
        return records;
    }


}
